package com.qingmeng.outsourcing.portal.controller;

import com.qingmeng.outsourcing.common.api.CommonPage;
import com.qingmeng.outsourcing.common.api.CommonResult;
import com.qingmeng.outsourcing.common.util.FileUtil;
import com.qingmeng.outsourcing.portal.model.dto.ProjectPublishParam;
import com.qingmeng.outsourcing.portal.model.vo.ProjectListVo;
import com.qingmeng.outsourcing.portal.service.ProjectService;
import com.qingmeng.outsourcing.portal.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "ProjectController")
@RestController
@RequestMapping("/project")

public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @ApiOperation("发布项目")
    @PostMapping(value = "/publishTask")
    public CommonResult<Integer> publishTask(@ApiParam("项目发布时填写的信息")@RequestBody ProjectPublishParam param){
        int result=projectService.publishTack(param);
        if (result>0){
            return  CommonResult.success(result,"项目发布成功");
        }
        return CommonResult.failed("项目发布不成功");
    }
    @ApiOperation("上传项目相关文件")
    @PostMapping(value = "/uploadFile")
    public CommonResult<String> uploadFile(@ApiParam("项目相关文件") @RequestParam("file")MultipartFile file)throws Exception{
        Long userId= SecurityUtils.getCurrentUser().getId();
        String filePath= FileUtil.portalUploadFileByUserId(userId,file,2);
        if (filePath!=null){
            return CommonResult.success(filePath,"文件上传成功");
        }
        return CommonResult.failed("文件上传失败");

    }
    @ApiOperation("获取任务大厅所需项目列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<ProjectListVo>> list(@ApiParam("页码") @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                                        @ApiParam("每页包含的条数") @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        List<ProjectListVo> projectListVos=projectService.list(pageNum,pageSize);
        if (projectListVos!=null&&projectListVos.size()>0){
            return CommonResult.success(CommonPage.restPage(projectListVos));
        }
        return CommonResult.failed("没有查到相关数据");

    }


}
